/*
 * Detached Quercus 
 * 
 * BSD license
 * 
 * Copyright (c) 2009 by Yauhen Yakimovich
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *  3. The name of the author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY HABELITZ SOFTWARE DEVELOPMENTS ('HSD') ``AS IS'' 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL 'HSD' BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
package org.phpandallthat;

import java.io.IOException;
import java.util.logging.Logger;

import com.caucho.quercus.Quercus;
import com.caucho.quercus.env.Env;
import com.caucho.quercus.env.Value;
import com.caucho.quercus.page.QuercusPage;
import com.caucho.vfs.FilePath;
import com.caucho.vfs.StdoutStream;
import com.caucho.vfs.WriteStream;

/**
 * Detached Quercus (PHP implementation in Java)
 * 
 * CLI - Command Line Interpreter
 * 
 * @author Yauhen Yakimovich (at) gmail.com
 * 
 */
public class PhpCli {

	private static final Logger log = Logger.getLogger(PhpCli.class.getName());

	private Quercus phpFacade;

	private static final String VERSION = "0.1";

	private String fileName;

	private boolean verbose;

	public PhpCli() {
		phpFacade = new Quercus();
		phpFacade.init();
		phpFacade.setCompile(true);
	}

	private void version() {
		System.err
				.println("Detached Quercus (PHP implementation in Java) Version "
						+ VERSION);
		System.err.println("Quercus Version " + phpFacade.getVersion() + "; "
				+ "PHP Version " + phpFacade.getPhpVersion());
	}

	private void help() {
		version();
		System.err
				.println("usage: java org.phpandallthat.PhpCli [args] [phpfile]");
		System.err
				.println("  -verbose              generate version and other information");
		System.err
				.println("  -version              print the version and exit");
		System.err.println("  -help		            print the help and exit.");
	}

	private boolean processArgs(String[] args) {
		for (String arg : args) {
			if (arg.equals("-verbose")) {
				this.verbose = true;
			} else if (arg.equals("-version")) {
				version();
				return true;
			} else if (arg.equals("-help")) {
				help();
				return true;
			}
		}
		fileName = args[args.length - 1];
		return false;
	}

	private void exec() {
		QuercusPage compliedPhp = null;
		FilePath path = new FilePath(fileName);
		try {
			compliedPhp = phpFacade.parse(path);
		} catch (IOException e) {
			System.err.println("Exception occured while parsing a file:" + e);
			e.printStackTrace();
		}
		WriteStream out = new WriteStream(StdoutStream.create());
		Env env = phpFacade.createEnv(compliedPhp, out, null, null);
		Value res = compliedPhp.executeTop(env);
		try {
			out.flushBuffer();
		} catch (IOException e) {
			System.err.println(e);
			e.printStackTrace();
		}
		// System.out.print(res.toString());
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		PhpCli cli = new PhpCli();

		if (args.length > 0) {
			if (cli.processArgs(args))
				return;
		} else {
			cli.help();
			return;
		}

		cli.exec();
	}

}
